package com.hengfoxwolf.jnidemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

abstract class BaseActivity<VB : ViewBinding>(open val block: (LayoutInflater) -> VB) : AppCompatActivity(),CoroutineScope by MainScope() {

    private lateinit var _binding: VB
    protected val mBinding get() = _binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = block(layoutInflater)
        setContentView(_binding.root)
        init(savedInstanceState)
    }

    protected abstract fun init(savedInstanceState: Bundle?)

    operator fun TextView.provideDelegate(value:Any?, property: KProperty<*>) = object : ReadWriteProperty<Any?, String?> {
        override fun getValue(thisRef: Any?, property: KProperty<*>): String? {
            return text as String?
        }

        override fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
            text = value
        }
    }
}